﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmDMThanhVien

    Private _IDThanhVien As String
    Public Property IDThanhVien() As String
        Get
            Return _IDThanhVien
        End Get
        Set(ByVal value As String)
            _IDThanhVien = value
        End Set
    End Property

    Private _LaChon As Boolean
    Public Property LaChon() As Boolean
        Get
            Return _LaChon
        End Get
        Set(ByVal value As Boolean)
            _LaChon = value
        End Set
    End Property

    Private Sub FrmDMThanhVien_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        If e.KeyCode = Keys.Enter And LaChon Then
            If grvThanhVien.FocusedRowHandle < 0 Then
                Return
            End If
            IDThanhVien = grvThanhVien.GetRowCellValue(grvThanhVien.FocusedRowHandle, grdColID).ToString
            Me.Close()
        End If
        'Thêm mới
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pADD, btnThemMoi) Then
            btnThemMoi_ItemClick(Nothing, Nothing)
        End If
        'Sửa đổi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEDIT, btnSuaDoi) Then
            btnSuaDoi_ItemClick(Nothing, Nothing)
        End If
        'Xóa
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pDELETE, btnXoa) Then
            btnXoa_ItemClick(Nothing, Nothing)
        End If
        'Xuất
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXPORT, btnXuatExcel) Then
            btnXuatExcel_ItemClick(Nothing, Nothing)
        End If
        'Nạp lại
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pREFRESH, btnNapLai) Then
            btnNapLai_ItemClick(Nothing, Nothing)
        End If
    End Sub
    Public Shared Them, Sua, Xoa, SaoChep As Boolean
    Private Sub PhanQuyen()
        btnThemMoi.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSuaDoi.Visibility = IIf(Sua, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnXoa.Visibility = IIf(Xoa, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
    End Sub
    Private Sub FrmDMThanhVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not LaChon Then
            btnChon.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        End If
        FillDataToGrid()
        PhanQuyen()
    End Sub
    Private Sub FillDataToGrid()
        Try
            grdThanhVien.DataSource = (From ds In dbTV.DM_ThanhViens Where ds.InUsed Order By ds.MaThanhVien Select New With { _
                                       .ID = ds.ID, .MaThanhVien = ds.MaThanhVien, .TenThanhVien = ds.HoTenThanhVien, .DiaChi = ds.DiaChi, _
                                       .DienThoai = ds.SoDT, .CapBac = ds.CapBac.GetValueOrDefault, .Diem = ds.Diem, _
                                       .TienThanhToan = ds.TienThanhToan.GetValueOrDefault, .NgaySinh = ds.NgaySinh}).ToList
            grdThanhVien.RefreshDataSource()

        Catch ex As Exception

        End Try
    End Sub
    Private Sub btnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemMoi.ItemClick
        Dim frm As New FrmCapNhatThanhVien
        frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
        frm.Action = vAddAction
        frm.ShowDialog()
        FillDataToGrid()
    End Sub

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub btnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSuaDoi.ItemClick
        If grvThanhVien.FocusedRowHandle < 0 Then
            Return
        End If
        Dim strID = grvThanhVien.GetRowCellValue(grvThanhVien.FocusedRowHandle, grdColID).ToString
        Dim objThanhVien = (From ds In dbTV.DM_ThanhViens Where ds.InUsed AndAlso ds.ID = strID).FirstOrDefault
        If objThanhVien IsNot Nothing Then
            Dim frm As New FrmCapNhatThanhVien
            frm.Action = vEditAction
            frm.objThanhVien = objThanhVien
            frm.StartPosition = FormStartPosition.CenterScreen
            frm.ShowDialog()
            FillDataToGrid()
        End If
        
    End Sub

    Private Sub btnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoa.ItemClick
        Try

            If grvThanhVien.FocusedRowHandle < 0 Then
                Return
            End If
            Dim strID = grvThanhVien.GetRowCellValue(grvThanhVien.FocusedRowHandle, grdColID).ToString
            Dim objThanhVien = (From ds In dbTV.DM_ThanhViens Where ds.InUsed AndAlso ds.ID = strID).FirstOrDefault
            If objThanhVien IsNot Nothing Then
                'Dim obj1 = (From ds In dbTV.HDBanLes Where ds.InUsed AndAlso ds.IDThanhVien = objThanhVien.ID Select ds).FirstOrDefault
                'If obj1 IsNot Nothing Then
                '    MsgInfo("Tồn tại thành viên trong hóa đơn")
                '    Return
                'End If
                If MsgAskDelete("Bạn có muốn xóa thành viên này không?") = vMsg.vReSult.Yes Then
                    objThanhVien.InUsed = False
                    objThanhVien.EditedOn = dbTV.GetDateTimeServer
                    iSubmitChange()
                    FillDataToGrid()
                End If
            End If

        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub btnNapLai_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNapLai.ItemClick
        FillDataToGrid()
    End Sub

    Private Sub btnXuatExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuatExcel.ItemClick
        Export_Dialog(SaveFileDialog1, grdThanhVien)
    End Sub
End Class